/* 
 * Copyright IBM Japan, AMS Services 2003 ALL Rights Reserved.
 * 
 * This product includes software developed by 
 * the Apache Software Foundation (http://www.apache.org/).
 */
package com.tianqu.tms.core.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import com.tianqu.LicensePlateConfigConstant;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.validator.Field;
import org.apache.commons.validator.GenericTypeValidator;
import org.apache.commons.validator.GenericValidator;
import org.apache.commons.validator.ValidatorAction;
import org.apache.commons.validator.util.ValidatorUtils;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.validator.FieldChecks;
import org.apache.struts.validator.Resources;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.webservice.TmsCarGateService;

/**
 * validation定义
 * 
 * @author liugang
 * 
 */
public class TmsFieldChecks extends FieldChecks {
	private static Logger LOGGER = Logger.getLogger(TmsFieldChecks.class);
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * 初始化
	 */
	private TmsFieldChecks() {
		super();
	}

	/**
	 * 获得录入信息内容
	 * 
	 * @param bean
	 * @param field
	 * @return
	 */
	private static String getFieldValueAsString(Object bean, Field field) {
		if (isString(bean)) {
			return (String) bean;
		} else {
			return ValidatorUtils.getValueAsString(bean, field.getProperty());
		}
	}

	/**
	 * 类型是object的对象取得
	 * 
	 * @param bean
	 * @param property
	 * @return
	 */
	public static Object getPropertyAsObject(Object bean, String property) {
		Object value = null;
		try {
			value = PropertyUtils.getProperty(bean, property);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return value;
	}

	/**
	 * 根据boolean型取得属性值
	 * 
	 * @param bean
	 * @param property
	 * @return
	 */
	public static boolean getPropertyAsBooleanValue(Object bean, String property) {
		boolean value = false;
		try {
			value = ((Boolean) PropertyUtils.getProperty(bean, property)).booleanValue();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return value;
	}

	/**
	 * 判定对象是不是空，是不是null，是不是false
	 */
	public static boolean isBlankOrNullOrFalse(Object value) {
		if (isBoolean(value)) {
			boolean rtn = !(((Boolean) value).booleanValue());
			return rtn;
		}
		return GenericValidator.isBlankOrNull((String) value);
	}

	/**
	 * 指定的对象是不是boolean型
	 * 
	 * @param value
	 * @return
	 */
	private static boolean isBoolean(Object value) {
		return Boolean.class.isInstance(value);
	}

	/*-------------------- Public methods --------------------*/

	/**
	 * 验证VIN码
	 */
	public static boolean validateVin(Object bean, ValidatorAction va, Field field, ActionMessages errors,
			HttpServletRequest request) {
		// 取得页面的值
		String value = getFieldValueAsString(bean, field);
		int isHz = 0;
		// 值不为空或者null
		if (!StringUtil.isNullOrBlank(value)) {
			try {
				// 值是英数字的check
				if (!FieldChecker.isAlphabetNumericalSt(value) || value.length()<6 || value.length() >17) {
					errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
					return false;
				}
			} catch (Exception e) {
				errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
				return false;
			}
		}
		return true;
	}

	/**
	 * add by huangwei 正则表达式
	 * 
	 * @param carLicense
	 *            v305
	 * @return
	 */
	private static boolean pattern(String value, String regex) {
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(value);
		return m.matches();
	}

	/**
	 * 验证车牌号码 v305 add by huangwei
	 * 
	 * @param carLicens
	 * @return
	 */
	public static boolean isCarLicense(String carLicense) {
		try {
			if (!StringUtil.isNullOrBlank(carLicense)) {
				String firstValue = carLicense.substring(0, 1);
				String secondValue = carLicense.substring(1, 2);
				String otherValue = carLicense.substring(2, carLicense.length());
				if (!FieldChecker.checkLength(carLicense, 7, 7))
					return false; // 长度是7位的check
				//
				if (TmsFieldChecks.pattern(carLicense, "[a-zA-Z]{2}[0-9]{5}"))
					return true; // 军用车牌（由两个字母+5位数字组成）
				//
				else if (FieldChecker.isProvinceSt(firstValue) && FieldChecker.isAlphabetSt(secondValue)
						&& TmsFieldChecks.pattern(otherValue, "^[0-9a-zA-Z]{4}[0-9a-zA-Z警学教领挂]{1}")) {
					return true;
				} else {
					LOGGER.info("isCarLicense result :false,carLicense:" + carLicense);
					return false;
				}
			}
		} catch (Exception e) {
			LOGGER.error("isCarLicense error!", e);
			return false;
		}
		return true;
	}

	/**
	 * 验证车牌号码
	 */
	public static boolean validateCarLicense(Object bean, ValidatorAction va, Field field, ActionMessages errors,
			HttpServletRequest request) {
		// 取得页面的值
		// modify by huangwei
		if (!TmsFieldChecks.isCarLicense(getFieldValueAsString(bean, field))) {
			errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
			return false;
		}

		// // 值不为空或者null
		// if (!StringUtil.isNullOrBlank(value)) {
		// try {
		// // 长度是7位的check
		// if (!FieldChecker.checkLength(value, 7, 7)) {
		// errors.add(field.getKey(), Resources.getActionMessage(
		// request, va, field));
		// return false;
		// }
		// String firstValue = value.substring(0, 1);
		// String secondValue = value.substring(1, 2);
		// String otherValue = value.substring(2, value.length());
		// // 第一个字母是否是指定值
		// if (!FieldChecker.isProvinceSt(firstValue)) {
		// errors.add(field.getKey(), Resources.getActionMessage(
		// request, va, field));
		// return false;
		// }
		// // 第二个字母是否是指定值
		// if (!FieldChecker.isAlphabetSt(secondValue)) {
		// errors.add(field.getKey(), Resources.getActionMessage(
		// request, va, field));
		// return false;
		// }
		// //后五位验证
		// String el="^[0-9a-zA-Z]{4}[0-9a-zA-Z挂学警军港澳]{1}";
		// Pattern p = Pattern.compile(el);
		// Matcher m = p.matcher(otherValue);
		// boolean dateFlag = m.matches();
		// if (!dateFlag) {
		// errors.add(field.getKey(), Resources.getActionMessage(
		// request, va, field));
		// return false;
		// }
		// } catch (Exception e) {
		// errors.add(field.getKey(), Resources.getActionMessage(request,
		// va, field));
		// return false;
		// }
		// }
		return true;
	}

	/**
	 * 验证车牌号码
	 */
	public static boolean validateCarLicense1(Object bean, ValidatorAction va, Field field, ActionMessages errors,
			HttpServletRequest request) {
		// 取得页面的值
		String value = getFieldValueAsString(bean, field);
		// 值不为空或者null
		if (!StringUtil.isNullOrBlank(value)) {
			try {
				if (LicensePlateConfigConstant.defLicensePlate.equals(value)) {
					return true;
				} else if (!TmsFieldChecks.isCarLicense(getFieldValueAsString(bean, field))) {
					errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
					return false;
				}

			} catch (Exception e) {
				errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
				return false;
			}
		}
		return true;
	}

	/**
	 * 验证车牌型号
	 */
	public static boolean validateModelCode(Object bean, ValidatorAction va, Field field, ActionMessages errors,
			HttpServletRequest request) {
		// 取得页面的值
		String value = getFieldValueAsString(bean, field);
		// 值不为空或者null
		if (!StringUtil.isNullOrBlank(value)) {
			// 长度是1~30位的check
			if (!FieldChecker.checkLength(value, 1, 30)) {
				errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
				return false;
			}
		}
		return true;
	}

	/**
	 * 验证车辆厂牌
	 */
	public static boolean validateVendorCode(Object bean, ValidatorAction va, Field field, ActionMessages errors,
			HttpServletRequest request) {
		// 取得页面的值
		String value = getFieldValueAsString(bean, field);
		// 值不为空或者null
		if (!StringUtil.isNullOrBlank(value)) {
			// 长度是1~30位的check
			if (!FieldChecker.checkLength(value, 1, 30)) {
				errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
				return false;
			}
		}
		return true;
	}

	/**
	 * 验证车辆表显里程
	 */
	public static boolean validateCarMileage(Object bean, ValidatorAction va, Field field, ActionMessages errors,
			HttpServletRequest request) {
		// 取得页面的值
		String value = getFieldValueAsString(bean, field).replace(",", "");
		// 值不为空或者null

		try {
			// 值是数量的check
			if (!FieldChecker.isNumber1(value)) {
				errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
				return false;
			}
		} catch (Exception e) {
			errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
			return false;
		}

		return true;
	}

	/**
	 * 验证日期（登记日期 发证日期）日期格式而且要是之前的日期
	 */
	public static boolean validateCarDateBeforNow(Object bean, ValidatorAction va, Field field, ActionMessages errors,
			HttpServletRequest request) {
		// 取得页面的值
		String datePatternStrict = field.getVarValue("datePattern");
		String value = getFieldValueAsString(bean, field);
		// 值不为空或者null
		if (!StringUtil.isNullOrBlank(value)) {
			try {
				// yyyy-MM-dd日期格式的check
				String eL = "(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)";
				Pattern p = Pattern.compile(eL);
				Matcher m = p.matcher(value);
				boolean dateFlag = m.matches();
				if (!dateFlag) {
					errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
					return false;
				}
				// 验证是否是之前日期
				return validateBeforeToday(bean, va, field, errors, request);
			} catch (Exception e) {
				errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
				return false;
			}
		}
		return true;
	}

	/**
	 * 验证日期 YYYY-MM
	 */
	public static boolean validateCarMonth(Object bean, ValidatorAction va, Field field, ActionMessages errors,
			HttpServletRequest request) {
		// 取得页面的值
		String datePatternStrict = field.getVarValue("datePattern");
		String value = getFieldValueAsString(bean, field);
		// 值不为空或者null
		if (!StringUtil.isNullOrBlank(value)) {
			try {
				// yyyy-MM-dd日期格式的check
				String eL = "[0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3}-?(?:0[1-9]|1[0-2])$";
				Pattern p = Pattern.compile(eL);
				Matcher m = p.matcher(value);
				boolean dateFlag = m.matches();
				if (!dateFlag) {
					errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
					return false;
				}
			} catch (Exception e) {
				errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
				return false;
			}
		}
		return true;
	}

	/**
	 * 验证日期 YYYY
	 */
	public static boolean validateCarYear(Object bean, ValidatorAction va, Field field, ActionMessages errors,
			HttpServletRequest request) {
		// 取得页面的值
		String datePatternStrict = field.getVarValue("datePattern");
		String value = getFieldValueAsString(bean, field);
		// 值不为空或者null
		if (!StringUtil.isNullOrBlank(value)) {
			try {
				// yyyy-MM-dd日期格式的check
				String eL = "[0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3}";
				Pattern p = Pattern.compile(eL);
				Matcher m = p.matcher(value);
				boolean dateFlag = m.matches();
				if (!dateFlag) {
					errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
					return false;
				}
			} catch (Exception e) {
				errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
				return false;
			}
		}
		return true;
	}

	/**
	 * 验证日期（登记日期 发证日期）日期格式而且要是之前的日期
	 */
	public static boolean validateJsDateTime(Object bean, ValidatorAction va, Field field, ActionMessages errors,
			HttpServletRequest request) {
		// 取得页面的值
		String value = getFieldValueAsString(bean, field);
		// 值不为空或者null
		if (!StringUtil.isNullOrBlank(value)) {
			try {
				// yyyy-MM-dd日期格式的check
				Pattern p = Pattern.compile(
						"^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)\\s+([01][0-9]|2[0-3]):[0-5][0-9]");
				Matcher m = p.matcher(value);
				boolean dateFlag = m.matches();
				if (!dateFlag) {
					errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
					return false;
				}
			} catch (Exception e) {
				errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
				return false;
			}
		}
		return true;
	}

	/**
	 * 验证日期（ 年检日期 交强险日期 ）
	 */
	public static boolean validateCarDate(Object bean, ValidatorAction va, Field field, ActionMessages errors,
			HttpServletRequest request) {
		// 取得页面的值
		String value = getFieldValueAsString(bean, field);
		String datePatternStrict = field.getVarValue("datePattern");
		// 值不为空或者null
		if (!StringUtil.isNullOrBlank(value)) {
			try {
				// yyyy-MM-dd日期格式的check
				String eL = "(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)";
				Pattern p = Pattern.compile(eL);
				Matcher m = p.matcher(value);
				boolean dateFlag = m.matches();
				if (!dateFlag) {
					errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
					return false;
				}
			} catch (Exception e) {
				errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
				return false;
			}
		}
		return true;
	}

	/**
	 * 验证发动机号
	 */
	public static boolean validateCarEngine(Object bean, ValidatorAction va, Field field, ActionMessages errors,
			HttpServletRequest request) {
		// 取得页面的值
		String value = getFieldValueAsString(bean, field);
		// 值不为空或者null
		if (!StringUtil.isNullOrBlank(value)) {
			try {
				// 长度是1~30位的check
				if (!FieldChecker.checkLength(value, 1, 30)) {
					errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
					return false;
				}
			} catch (Exception e) {
				errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
				return false;
			}
		}
		return true;
	}

	/**
	 * 备考
	 * 
	 * @param bean
	 * @param va
	 * @param field
	 * @param errors
	 * @param request
	 * @return
	 */
	public static boolean validateRemarks(Object bean, ValidatorAction va, Field field, ActionMessages errors,
			HttpServletRequest request) {
		// 取得页面的值
		String value = getFieldValueAsString(bean, field);
		// 值不为空或者null
		if (!StringUtil.isNullOrBlank(value)) {
			try {
				// 中文
				if (FieldChecker.validateValidSt(value, FieldChecker.SYMBOL)) {
					errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
					return false;
				}
			} catch (Exception e) {
				errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
				return false;
			}
		}
		return true;
	}

	/**
	 * 验证登记证书号
	 */
	public static boolean validateRegisterCardNo(Object bean, ValidatorAction va, Field field, ActionMessages errors,
			HttpServletRequest request) {
		// 取得页面的值
		String value = getFieldValueAsString(bean, field);
		// 值不为空或者null
		if (!StringUtil.isNullOrBlank(value)) {
			try {
				// 长度是17~20位的check
				if (!FieldChecker.checkLength(value, 1, 12)) {
					errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
					return false;
				}
				// 数字,字母check
				if (!FieldChecker.isAlphabetNumericalSt(value)) {
					errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
					return false;
				}
			} catch (Exception e) {
				errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
				return false;
			}
		}
		return true;
	}

	/**
	 * 用户名check
	 */
	public static boolean validateRegisterUserName(Object bean, ValidatorAction va, Field field, ActionMessages errors,
			HttpServletRequest request) {
		// 取得页面的值
		String value = getFieldValueAsString(bean, field);
		// 值不为空或者null
		if (!StringUtil.isNullOrBlank(value)) {
			try {
				// 数字,字母check
				if (!FieldChecker.isAlphabetNumericalSt(value)) {
					errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
					return false;
				}
			} catch (Exception e) {
				errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
				return false;
			}
		}
		return true;
	}

	/**
	 * 用户名check
	 */
	public static boolean validateCarOwnerIdCardNo(Object bean, ValidatorAction va, Field field, ActionMessages errors,
			HttpServletRequest request) {
		// 取得页面的值
		String value = getFieldValueAsString(bean, field);
		// 值不为空或者null
		if (!StringUtil.isNullOrBlank(value)) {
			try {
				// 数字,字母check
				if (!FieldChecker.isAlphabetNumericalSt(value) && !"-".equals(value)) {
					errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
					return false;
				}
			} catch (Exception e) {
				errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
				return false;
			}
		}
		return true;
	}

	/**
	 * 验证页面固定电话
	 * 
	 * @param bean
	 * @param va
	 * @param field
	 * @param errors
	 * @param request
	 * @return
	 */
	public static boolean validateFixedTelephone(Object bean, ValidatorAction va, Field field, ActionMessages errors,
			HttpServletRequest request) {
		// 取得页面的值
		String value = getFieldValueAsString(bean, field);
		// 值不为空或者null
		if (!StringUtil.isNullOrBlank(value)) {
			try {
				// 固定电话的正则验证
				Pattern p = Pattern.compile("1([\\d]{10})((\\+[0-9]{2,4})?\\(?[0-9]+\\)?-?)?[0-9]{7,8}");
				Matcher m = p.matcher(value);
				boolean dateFlag = m.matches();
				if (!dateFlag) {
					errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
					return false;
				}
			} catch (Exception e) {
				errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
				return false;
			}
		}
		return true;

	}

	/**
	 * 核定载质量
	 * 
	 * @param bean
	 * @param va
	 * @param field
	 * @param errors
	 * @param request
	 * @return
	 */
	public static boolean validateCarLoadquality(Object bean, ValidatorAction va, Field field, ActionMessages errors,
			HttpServletRequest request) {
		// 取得页面的值
		String value = getFieldValueAsString(bean, field);
		// 值不为空或者null
		if (!StringUtil.isNullOrBlank(value)) {
			try {
				// float的check
				if (!GenericValidator.isFloat(value)) {
					errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
					return false;
				}
			} catch (Exception e) {
				errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
				return false;
			}
		}
		return true;
	}

	/**
	 * 核定载人数
	 */
	public static boolean validateCarPassenger(Object bean, ValidatorAction va, Field field, ActionMessages errors,
			HttpServletRequest request) {
		// 取得页面的值
		String value = getFieldValueAsString(bean, field);
		// 值不为空或者null
		if (!StringUtil.isNullOrBlank(value)) {
			try {
				// int的check
				if (!FieldChecker.isNumber(value)) {
					errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
					return false;
				}
			} catch (Exception e) {
				errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
				return false;
			}
		}
		return true;
	}

	/**
	 * 预售价格(元)
	 * 
	 * @param bean
	 * @param va
	 * @param field
	 * @param errors
	 * @param request
	 * @return
	 */
	public static boolean validateCarSellPrice(Object bean, ValidatorAction va, Field field, ActionMessages errors,
			HttpServletRequest request) {
		// 取得页面的值
		String value = getFieldValueAsString(bean, field).replace(",", "");

		// 值不为空或者null
		if (!StringUtil.isNullOrBlank(value)) {
			try {
				if ("0".equals(value)) {
					return true;
				}
				// int的check
				if (!FieldChecker.isNumber(value)) {
					errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
					return false;
				}
			} catch (Exception e) {
				errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
				return false;
			}
		}
		return true;
	}

	/**
	 * 人名
	 * 
	 * @param bean
	 * @param va
	 * @param field
	 * @param errors
	 * @param request
	 * @return
	 */
	public static boolean validateUserName(Object bean, ValidatorAction va, Field field, ActionMessages errors,
			HttpServletRequest request) {
		// 取得页面的值
		String value = getFieldValueAsString(bean, field);
		// 值不为空或者null
		if (!StringUtil.isNullOrBlank(value)) {
			try {
				// 中文
				if (!FieldChecker.isChineseSt(value)) {
					errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
					return false;
				}
			} catch (Exception e) {
				errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
				return false;
			}
		}
		return true;
	}

	/**
	 * 车主身份证号/买主身份证号
	 */
	public static boolean validateCardNo(Object bean, ValidatorAction va, Field field, ActionMessages errors,
			HttpServletRequest request) {
		// 取得页面的值
		String value = getFieldValueAsString(bean, field);
		// 值不为空或者null
		if (!StringUtil.isNullOrBlank(value)) {
			// 长度是18位的check
			if (!FieldChecker.checkLength(value, 18, 18)) {
				errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
				return false;
			}
			// 18位英数字的check(前17是数字，18位是英数字)
			if (!GenericValidator.matchRegexp(value, "^[0-9]{17}[0-9a-zA-Z]{1}$")) {
				errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
				return false;
			}
		} else {
			return true;
		}
		return true;
	}

	/**
	 * 组织机构代码证号
	 */
	public static boolean validateOrgNo(Object bean, ValidatorAction va, Field field, ActionMessages errors,
			HttpServletRequest request) {
		// 取得页面的值
		String value = getFieldValueAsString(bean, field);
		if (!StringUtil.isNullOrBlank(value) && value.indexOf("-") > -1) {
			value = value.replace("-", "");
		}
		// 值不为空或者null
		if (!StringUtil.isNullOrBlank(value)) {
			// 长度是1~30位的check
			if (!FieldChecker.checkLength(value, 1, 30)) {
				errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
				return false;
			}
			// 18位英数字的check(前17是数字，18位是英数字)
			if (!FieldChecker.isAlphabetNumericalSt(value)) {
				errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
				return false;
			}
		}
		return true;
	}

	/**
	 * 联系电话/车主电话/买主电话
	 */
	public static boolean validatePhone(Object bean, ValidatorAction va, Field field, ActionMessages errors,
			HttpServletRequest request) {
		// 取得页面的值
		String value = getFieldValueAsString(bean, field);
		// 值不为空或者null
		if (!StringUtil.isNullOrBlank(value)) {
			// 长度是1~20位的check
			if (!FieldChecker.checkLength(value, 1, 20)) {
				errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
				return false;
			}
			// 值是数字
			if (!FieldChecker.isNumericalSt(value)) {
				errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
				return false;
			}
		}
		return true;
	}

	/**
	 * 联系电话
	 */
	public static boolean validateOwnerPhone(Object bean, ValidatorAction va, Field field, ActionMessages errors,
			HttpServletRequest request) {
		// 取得页面的值
		String value = getFieldValueAsString(bean, field);
		// 值不为空或者null
		if (!StringUtil.isNullOrBlank(value)) {
			// 长度是11位的check
			if (!FieldChecker.checkLength(value, 11, 11)) {
				errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
				return false;
			}
			// 值是数字
			if (!FieldChecker.isNumericalSt(value)) {
				errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
				return false;
			}
		}
		return true;
	}

	/**
	 * 判定录入信息是否达到最长要求（英文和汉字都作为一个字符长度）
	 * 
	 * @param bean
	 * @param va
	 * @param field
	 * @param errors
	 * @param request
	 * @return
	 */
	public static boolean validateMaxLength(Object bean, ValidatorAction va, Field field, ActionMessages errors,
			HttpServletRequest request) {

		String value = getFieldValueAsString(bean, field);
		if (!StringUtil.isNullOrBlank(value)) {
			try {
				int max = Integer.parseInt(field.getVarValue("maxlength"));
				if (!GenericValidator.maxLength(value, max)) {
					errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
					return false;
				}
			} catch (Exception e) {
				errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
				return false;
			}
		}
		return true;
	}

	/**
	 * 判定输入信息是否达到最小长度要求
	 * 
	 * @param bean
	 * @param va
	 * @param field
	 * @param errors
	 * @param request
	 * @return
	 */
	public static boolean validateMinLength(Object bean, ValidatorAction va, Field field, ActionMessages errors,
			HttpServletRequest request) {

		String value = getFieldValueAsString(bean, field);
		if (!StringUtil.isNullOrBlank(value)) {
			try {
				int min = Integer.parseInt(field.getVarValue("minlength"));
				if (!GenericValidator.minLength(value, min)) {
					errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
					return false;
				}
			} catch (Exception e) {
				errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
				return false;
			}
		}

		return true;
	}

	/**
	 * 判定录入信息是否达到最长录入要求（字节为标准，字母作为一个字符长度，汉字作为两个字符长度）
	 * 
	 * @param bean
	 * @param va
	 * @param field
	 * @param errors
	 * @param request
	 * @return
	 */
	public static boolean validateMaxLengthByBytes(Object bean, ValidatorAction va, Field field, ActionMessages errors,
			HttpServletRequest request) {

		String value = getFieldValueAsString(bean, field);
		if (!StringUtil.isNullOrBlank(value)) {
			try {
				int max = Integer.parseInt(field.getVarValue("maxlength"));
				if (value.getBytes().length > max) {
					errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
					return false;
				}
			} catch (Exception e) {
				errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
				return false;
			}
		}
		return true;
	}

	/**
	 * 判定录入信息格式是否符合指定的正则表达式要求
	 * 
	 * @param bean
	 * @param va
	 * @param field
	 * @param errors
	 * @param request
	 * @return
	 */
	public static boolean validateMask(Object bean, ValidatorAction va, Field field, ActionMessages errors,
			HttpServletRequest request) {
		String mask = field.getVarValue("mask");
		String value = getFieldValueAsString(bean, field);
		try {
			if (!StringUtil.isNullOrBlank(value) && !GenericValidator.matchRegexp(value, mask)) {
				errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
				return false;
			} else {
				return true;
			}
		} catch (Exception e) {
			errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
			return false;
		}
	}

	/**
	 * 判定录入是否填写（null和空都作为没有填写）
	 * 
	 * @param bean
	 * @param va
	 * @param field
	 * @param errors
	 * @param request
	 * @return
	 */
	public static boolean validateRequired(Object bean, ValidatorAction va, Field field, ActionMessages errors,
			HttpServletRequest request) {
		String value = getFieldValueAsString(bean, field);
		if (StringUtil.isNullOrBlank(value)) {
			errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
			return false;
		} else {
			return true;
		}
	}

	/**
	 * 判断是否为空加入"0"，null，空
	 * 
	 * @param bean
	 * @param va
	 * @param field
	 * @param errors
	 * @param request
	 * @return
	 */
	public static boolean validateRequired1(Object bean, ValidatorAction va, Field field, ActionMessages errors,
			HttpServletRequest request) {
		String value = getFieldValueAsString(bean, field);
		if (StringUtil.isNullOrBlank(value)) {
			errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
			return false;
		} else if ("0".equals(value) || "0.0".equals(value)) {
			errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
			return false;
		} else {
			return true;
		}
	}

	/**
	 * 判定录入信息是不是short型
	 * 
	 * @param bean
	 * @param va
	 * @param field
	 * @param errors
	 * @param request
	 * @return
	 */
	public static Object validateShort(Object bean, ValidatorAction va, Field field, ActionMessages errors,
			HttpServletRequest request) {
		String value = getFieldValueAsString(bean, field);
		if (StringUtil.isNullOrBlank(value)) {
			return Boolean.TRUE;
		}
		Object result = GenericTypeValidator.formatShort(value);
		if (result == null) {
			errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
		}
		return result == null ? Boolean.FALSE : result;
	}

	/**
	 * 判定录入信息是不是integer格式
	 * 
	 * @param bean
	 * @param va
	 * @param field
	 * @param errors
	 * @param request
	 * @return
	 */
	public static Object validateInteger(Object bean, ValidatorAction va, Field field, ActionMessages errors,
			HttpServletRequest request) {
		String value = getFieldValueAsString(bean, field);
		if (StringUtil.isNullOrBlank(value)) {
			return Boolean.TRUE;
		}
		Object result = GenericTypeValidator.formatInt(value);
		if (result == null) {
			errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
		}
		return result == null ? Boolean.FALSE : result;
	}

	/**
	 * 判定录入信息是不是long型
	 * 
	 * @param bean
	 * @param va
	 * @param field
	 * @param errors
	 * @param request
	 * @return
	 */
	public static Object validateLong(Object bean, ValidatorAction va, Field field, ActionMessages errors,
			HttpServletRequest request) {
		String value = getFieldValueAsString(bean, field);
		if (StringUtil.isNullOrBlank(value)) {
			return Boolean.TRUE;
		}
		Object result = GenericTypeValidator.formatLong(value);
		if (result == null) {
			errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
		}
		return result == null ? Boolean.FALSE : result;
	}

	/**
	 * 判定录入信息是否是float格式
	 * 
	 * @param bean
	 * @param va
	 * @param field
	 * @param errors
	 * @param request
	 * @return
	 */
	public static Object validateFloat(Object bean, ValidatorAction va, Field field, ActionMessages errors,
			HttpServletRequest request) {
		String value = getFieldValueAsString(bean, field);
		if (StringUtil.isNullOrBlank(value)) {
			return Boolean.TRUE;
		}
		Object result = GenericTypeValidator.formatFloat(value);
		if (result == null) {
			errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
		}
		return result == null ? Boolean.FALSE : result;
	}

	/**
	 * 判定录入信息是否是double格式
	 * 
	 * @param bean
	 * @param va
	 * @param field
	 * @param errors
	 * @param request
	 * @return
	 */
	public static Object validateDouble(Object bean, ValidatorAction va, Field field, ActionMessages errors,
			HttpServletRequest request) {
		String value = getFieldValueAsString(bean, field);
		if (StringUtil.isNullOrBlank(value)) {
			return Boolean.TRUE;
		}
		Object result = GenericTypeValidator.formatDouble(value);
		if (result == null) {
			errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
		}
		return result == null ? Boolean.FALSE : result;
	}

	/**
	 * 判定录入的Int型数据是否在指定范围内
	 * 
	 * @param bean
	 * @param va
	 * @param field
	 * @param errors
	 * @param request
	 * @return
	 */
	public static boolean validateIntRange(Object bean, ValidatorAction va, Field field, ActionMessages errors,
			HttpServletRequest request) {

		String value = getFieldValueAsString(bean, field);
		if (!StringUtil.isNullOrBlank(value)) {
			try {
				int intValue = Integer.parseInt(value);
				int min = Integer.parseInt(field.getVarValue("min"));
				int max = Integer.parseInt(field.getVarValue("max"));
				if (!GenericValidator.isInRange(intValue, min, max)) {
					errors.add(field.getKey(), Resources.getActionMessage(request, va, field));

					return false;
				}
			} catch (Exception e) {
				errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
				return false;
			}
		}

		return true;
	}

	/**
	 * 严重录入double格式信息是否在指定范围内
	 * 
	 * @param bean
	 * @param va
	 * @param field
	 * @param errors
	 * @param request
	 * @return
	 */
	public static boolean validateDoubleRange(Object bean, ValidatorAction va, Field field, ActionMessages errors,
			HttpServletRequest request) {

		String value = getFieldValueAsString(bean, field);
		if (!StringUtil.isNullOrBlank(value)) {
			try {
				double doubleValue = Double.parseDouble(value);
				double min = Double.parseDouble(field.getVarValue("min"));
				double max = Double.parseDouble(field.getVarValue("max"));
				if (!GenericValidator.isInRange(doubleValue, min, max)) {
					errors.add(field.getKey(), Resources.getActionMessage(request, va, field));

					return false;
				}
			} catch (Exception e) {
				errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
				return false;
			}
		}
		return true;
	}

	/**
	 * 判定录入的float格式信息是否在指定范围内。
	 * 
	 * @param bean
	 * @param va
	 * @param field
	 * @param errors
	 * @param request
	 * @return
	 */
	public static boolean validateFloatRange(Object bean, ValidatorAction va, Field field, ActionMessages errors,
			HttpServletRequest request) {
		String value = getFieldValueAsString(bean, field);
		if (!StringUtil.isNullOrBlank(value)) {
			try {
				float floatValue = Float.parseFloat(value);
				float min = Float.parseFloat(field.getVarValue("min"));
				float max = Float.parseFloat(field.getVarValue("max"));
				if (!GenericValidator.isInRange(floatValue, min, max)) {
					errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
					return false;
				}
			} catch (Exception e) {
				errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
				return false;
			}
		}
		return true;
	}

	/**
	 * 日期比较 beginDate <= endDate
	 * 
	 * @param bean
	 * @param va
	 * @param field
	 * @param errors
	 * @param request
	 * @return
	 */
	public static boolean validateCalendarDateCheck(Object bean, ValidatorAction va, Field field, ActionMessages errors,
			HttpServletRequest request) {

		// 获得值
		String datePatternStrict = field.getVarValue("datePattern");
		String beginDate = ValidatorUtils.getValueAsString(bean, field.getVarValue("beginDate"));
		String endDate = ValidatorUtils.getValueAsString(bean, field.getVarValue("endDate"));
		// 都为空时候
		if (StringUtil.isNullOrBlank(beginDate) && StringUtil.isNullOrBlank(endDate)) {
			return true;
		}
		try {
			SimpleDateFormat formatDate = new SimpleDateFormat(datePatternStrict);
			Date begin = formatDate.parse(beginDate);
			Date end = formatDate.parse(endDate);
			if (begin.compareTo(end) > 0) {
				errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
				return false;
			}
		} catch (java.text.ParseException e) {
			errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
			return false;
		}
		return true;
	}

	/**
	 * 以后日期check
	 */
	public static boolean validateAfterToday(Object bean, ValidatorAction va, Field field, ActionMessages errors,
			HttpServletRequest request) {
		String datePatternStrict = field.getVarValue("datePattern");
		String strDate = getFieldValueAsString(bean, field);
		if (StringUtil.isNullOrBlank(strDate)) {
			return true;
		}
		try {
			// 填写的日期
			SimpleDateFormat formatDate = new SimpleDateFormat(datePatternStrict);
			Date inputDate = formatDate.parse(strDate);
			// 今天的日期
			Date currentDate = formatDate.parse(formatDate.format(new Date(System.currentTimeMillis())));
			// 比较
			if (inputDate.compareTo(currentDate) < 0) {
				errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
				return false;
			}
		} catch (java.text.ParseException e) {
			errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
			return false;
		}
		return true;
	}

	/**
	 * 以前日期check
	 * 
	 * @param bean
	 * @param va
	 * @param field
	 * @param errors
	 * @param request
	 * @return
	 */
	public static boolean validateBeforeToday(Object bean, ValidatorAction va, Field field, ActionMessages errors,
			HttpServletRequest request) {
		String datePatternStrict = field.getVarValue("datePattern");
		String strDate = getFieldValueAsString(bean, field);
		if (StringUtil.isNullOrBlank(strDate)) {
			return true;
		}
		try {
			// 填写的日期
			SimpleDateFormat formatDate = new SimpleDateFormat(datePatternStrict);
			Date inputDate = formatDate.parse(strDate);
			// 今天的日期
			Date currentDate = formatDate.parse(formatDate.format(new Date(System.currentTimeMillis())));
			// 比较
			if (inputDate.compareTo(currentDate) > 0) {
				errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
				return false;
			}
		} catch (java.text.ParseException e) {
			errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
			return false;
		}
		return true;
	}

	/**
	 * 字符串是否为空
	 * 
	 * @param str
	 * @param len
	 * @return
	 */
	public static boolean isNull(String str) {
		if (str == null) {
			return false;
		}
		if ("".equals(str)) {
			return false;
		}
		return true;
	}

	/**
	 * 邮政编码验证，为数字，长度为6位
	 * 
	 * @param bean
	 * @param va
	 * @param field
	 * @param errors
	 * @param request
	 * @return
	 */
	public static boolean zipCode(Object bean, ValidatorAction va, Field field, ActionMessages errors,
			HttpServletRequest request) {
		String value = getFieldValueAsString(bean, field);
		if (!StringUtil.isNullOrBlank(value)) {
			try {
				// 长度是6位的check
				if (!FieldChecker.checkLength(value, 6, 6)) {
					errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
					return false;
				}
				// 值是数字
				if (!FieldChecker.isNumericalSt(value)) {
					errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
					return false;
				}
			} catch (Exception e) {
				errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
				return false;
			}
		}
		return true;
	}

	/**
	 * 增值税编号/交易发票验证 数字 长度为8
	 * 
	 * @param bean
	 * @param va
	 * @param field
	 * @param errors
	 * @param request
	 * @return
	 */
	public static boolean taxNo(Object bean, ValidatorAction va, Field field, ActionMessages errors,
			HttpServletRequest request) {
		String value = getFieldValueAsString(bean, field);
		if (!StringUtil.isNullOrBlank(value)) {
			try {
				// 长度是8位的check
				if (!FieldChecker.checkLength(value, 5, 20)) {
					errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
					return false;
				}
				// 值是数字
				if (!FieldChecker.isNumericalSt(value)) {
					errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
					return false;
				}
			} catch (Exception e) {
				errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
				return false;
			}
		}
		return true;
	}

	// 竞价管理修正 Start
	/**
	 * 竞价开始时间：格式为HH:MM，输入长度是5位，输入时间必须是比当前系统时间晚10分钟
	 * 
	 * @param bean
	 * @param va
	 * @param field
	 * @param errors
	 * @param request
	 * @return
	 */
	public static boolean validateStartTime(Object bean, ValidatorAction va, Field field, ActionMessages errors,
			HttpServletRequest request) {
		String value = getFieldValueAsString(bean, field);
		String hh = "";
		String mm = "";
		if (!StringUtil.isNullOrBlank(value)) {
			try {
				// 长度是5位的check
				if (value.length() != 5) {
					errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
					return false;
				}
				// 格式为HH:MM
				if (!value.substring(2, 3).equals(":")) {
					errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
					return false;
				}
				hh = value.substring(0, 2);
				mm = value.substring(3, 5);
				// 数字的判断
				if (!FieldChecker.isNumericalSt(hh) || !FieldChecker.isNumericalSt(mm)) {
					errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
					return false;
				} else {
					// 输入时间必须是比当前系统时间晚10分钟
					if (Integer.valueOf(hh) > 23 || Integer.valueOf(hh) < 0 || Integer.valueOf(mm) > 60
							|| Integer.valueOf(mm) < 0
							|| Integer.valueOf(hh) < Calendar.getInstance().get(Calendar.HOUR_OF_DAY)
							|| (Integer.valueOf(hh) == Calendar.getInstance().get(Calendar.HOUR_OF_DAY)
									&& Integer.valueOf(mm) < Calendar.getInstance().get(Calendar.MINUTE) + 10)) {
						errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
						return false;
					}
				}
			} catch (Exception e) {
				errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
				return false;
			}
		}
		return true;
	}

	/**
	 * 竞价时长：数字，输入长度是2位，并且只能输入10~60以内时长
	 * 
	 * @param bean
	 * @param va
	 * @param field
	 * @param errors
	 * @param request
	 * @return
	 */
	public static boolean validateAdl(Object bean, ValidatorAction va, Field field, ActionMessages errors,
			HttpServletRequest request) {
		String value = getFieldValueAsString(bean, field);
		if (!StringUtil.isNullOrBlank(value)) {
			try {
				// 长度是5位的check
				if (value.length() != 2) {
					errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
					return false;
				}
				// 数字的判断
				if (!FieldChecker.isNumericalSt(value)) {
					errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
					return false;
				} else {
					// 只能输入10~60以内时长
					if (Integer.valueOf(value) > 60 || Integer.valueOf(value) < 10) {
						errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
						return false;
					}
				}
			} catch (Exception e) {
				errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
				return false;
			}
		}
		return true;
	}
	// 竞价管理修正 End
}